﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace SAP
{
    public partial class P_PartOrder : System.Web.UI.Page
    {


        private static int GeneralID;
        private static string dbType;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dbType = Session[VSM.Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? VSM.Utils.AppConstants.VSM : VSM.Utils.AppConstants.DEALER;
                GeneralID = Request.QueryString["GeneralID"] == null ? -1 : Int32.Parse(Request.QueryString["GeneralID"]);
                int RequestType = Request.QueryString["requestType"] == null ? 0 : Int32.Parse(Request.QueryString["requestType"]);

                hf_GeneralID.Value = GeneralID.ToString();
                Services sv = new Services();
                string sql = " select gs.JobID, gs.Receptionist, gs.AdviserCode, gs.TechnicianCode, jk.Name as JobKind, pm.Name as PaymentMethod, ";
                sql += " gs.DateIn, gs.TimeIn, gs.ExpDelivery, gs.ExpTime, gs.StartDate, gs.StartTime, ";
                sql += " gs.VehicleCode, gs.VIN, gs.CustomerCode, ";
                sql += " adv.Name AdviserName, tec.Name TechnicianName, ";
                sql += " gs.FinishDate, gs.FinishTime, gs.ActualDelivery, gs.ActualDeliveryTime, gs.CustomerRequest, gs.Notice, ";
                sql += " gs.Odometer, gs.ServiceDate, gs.JobKind, ";
                sql += " gs.VehicleName, gs.Brand, gs.EngineNo, gs.RegNo, gs.Model, gs.ColorCode, gs.CustomerName, gs.OtherName, ";
                sql += " gs.Address, gs.Tel, gs.TaxCode, gs.ContactPerson, gs.ContactPhone ";
                sql += " from U_GeneralService gs ";
                sql += " left join VSM_ORG..[@JOBKINDSVC] jk on jk.Code = gs.JobKind";
                sql += " left join VSM_ORG..[@PAYMENTMETHODSVC] pm on pm.Code = gs.Payment ";
                sql += " left join VSM_ORG..[@TECHNICIAN] adv on gs.AdviserCode = adv.Code ";
                sql += " left join VSM_ORG..[@TECHNICIAN] tec on gs.TechnicianCode = tec.Code ";
                sql += " where gs.GeneralID = " + GeneralID.ToString();

                DataSet ds = sv.ExecuteQuery(dbType, sql);
                if (ds.Tables[0].Rows.Count > 0) {
                    DataRow dr = ds.Tables[0].Rows[0];

                    txt_VIN.Text = ds.Tables[0].Rows[0]["VIN"].ToString();
                    txt_Brand.Text = ds.Tables[0].Rows[0]["Brand"].ToString();
                    txt_VehicleName.Text = ds.Tables[0].Rows[0]["VehicleName"].ToString();
                    txt_EngineNo.Text = ds.Tables[0].Rows[0]["EngineNo"].ToString();
                    txt_RegNo.Text = ds.Tables[0].Rows[0]["RegNo"].ToString();

                    txt_CusName.Text = ds.Tables[0].Rows[0]["CustomerName"].ToString();
                    txt_Address.Text = ds.Tables[0].Rows[0]["Address"].ToString();
                   
                    txt_ContactPerson.Text = ds.Tables[0].Rows[0]["ContactPerson"].ToString();
                    txt_PhoneContact.Text = ds.Tables[0].Rows[0]["ContactPhone"].ToString();
                    
                    txtDate.Text = FormatDate(dr["ServiceDate"].ToString());
                    txt_PONo.Text = "PO-" + dr["JobID"].ToString();

                    DataSet dsDetail = sv.ExecuteQuery(dbType, "select ROW_NUMBER() over (ORDER BY DetailCode) RowNum, * from [U_GeneralServiceDetail] where IsSelected = 1 AND GeneralID = " + GeneralID.ToString());
                    grvPO.DataSource = dsDetail.Tables[0];
                    grvPO.DataBind();
                }

            }
        }

        private string FormatDate(string dateIn)
        {
            if (dateIn.Trim() == "")
                return "";
            DateTime pars = DateTime.Parse(dateIn);
            return pars.ToString("dd/MM/yyyy");

        }

        private void loadVehicleByVIN(string VIN)
        {
            if (VIN == "")
                return;
            Services sv = new Services();

            string sql = "select rn.DistNumber as VIN, rn.Notes as EngineNo, ms.Name as Model, ";
            sql += " cg.RegistrationNumber as RegNo, ";
            sql += " it.ItemName as VehicleName, rn.U_KeyNo as KeyNo, ms.Code as ModelCode, ";
            sql += " it.U_Color as ColorCode, rn.LotNumber as LotNo, it.UserText as Remarks ";
            sql += " from  OSRN rn ";
            sql += " join OITM it on it.ItemCode = rn.ItemCode ";
            sql += " join [@ModelSales] ms on ms.Code = it.U_ModelSales ";
            sql += " left join U_CustomerRegis cg on cg.VIN = rn.DistNumber ";
            sql += " where rn.DistNumber = '" + VIN + "' ";
            DataSet ds = sv.ExecuteQuery("VSM", sql);

            if (ds.Tables[0].Rows.Count > 0)
            {
                txt_VIN.Text = ds.Tables[0].Rows[0]["VIN"].ToString();
                txt_Brand.Text = ds.Tables[0].Rows[0]["Brand"].ToString();
                txt_VehicleName.Text = ds.Tables[0].Rows[0]["VehicleName"].ToString();
                txt_EngineNo.Text = ds.Tables[0].Rows[0]["EngineNo"].ToString();
                txt_RegNo.Text = ds.Tables[0].Rows[0]["RegNo"].ToString();
            }
        }

        private void loadVehicle(string vehicleID)
        {
            Services sv = new Services();
            DataSet ds = sv.ExecuteQuery(dbType, "select * from U_ServiceVehicle where VehicleID = " + vehicleID);
            if (ds.Tables[0].Rows.Count > 0)
            {
                txt_VIN.Text = ds.Tables[0].Rows[0]["VIN"].ToString();
                txt_Brand.Text = ds.Tables[0].Rows[0]["Brand"].ToString();
                txt_VehicleName.Text = ds.Tables[0].Rows[0]["VehicleName"].ToString();
                txt_EngineNo.Text = ds.Tables[0].Rows[0]["EngineNo"].ToString();
                txt_RegNo.Text = ds.Tables[0].Rows[0]["RegNo"].ToString();
            }
        }

        private void loadCustomer(string CustomerCode)
        {
            MasterData masterDataWS = new MasterData();
            DataSet ds = masterDataWS.GetCustomerByCode("VSM", CustomerCode);
            if (ds.Tables[0].Rows.Count > 0)
            {
                txt_CusName.Text = ds.Tables[0].Rows[0]["CardName"].ToString();
                txt_Address.Text = ds.Tables[0].Rows[0]["Address"].ToString();
                txt_ContactPerson.Text = ds.Tables[0].Rows[0]["U_ContactPerson"].ToString();
                txt_PhoneContact.Text = ds.Tables[0].Rows[0]["U_ContactPhone"].ToString();
                
            }
        }
    }
}